﻿using System;

namespace MvvmFx.Common.ViewModels.Commands
{
    /// <summary>
    /// RoutedViewModelCommand inherits from <see cref="IViewModelCommand"/> and implements the <see cref="ICommandHandler"/> interface.
    /// It doesn't contain it's own execution logic. Instead, it allows instances of the <see cref="Command"/> class
    /// to bind to it. They are executed when the RoutedViewModelCommand is instructed to execute.
    /// </summary>
    public class RoutedViewModelCommand : Command, IViewModelCommand
    {
        #region [ Field ]

        /// <summary>
        /// The CanExecuteChanged event fired.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// The name member to be bound to on the button.
        /// </summary>
        private readonly string name;

        #endregion

        #region [ Constructors ]

        /// <summary>
        /// Initializes a new instance of the <see cref="RoutedViewModelCommand"/> class.
        /// </summary>
        public RoutedViewModelCommand()
        {
            name = string.Empty;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RoutedViewModelCommand"/> class.
        /// </summary>
        /// <param name="name">The name of the </param>
        public RoutedViewModelCommand(string name)
        {
            this.name = name;
        }

        #endregion

        #region [ Properties ]

        /// <summary>
        /// The name of the command which well show when it is bound to a button.
        /// </summary>
        public string Name
        {
            get { return name; }
        }

        #endregion

        #region [ Public Methods ]

        /// <summary>
        /// Execute the RoutedUICommand's registered command bindings.
        /// </summary>
        public void Execute()
        {
            this.NotifyInstanceHandlers(null);
        }

        /// <summary>
        /// Execute the RoutedUICommand's registered command bindings.
        /// </summary>
        /// <param name="parameter">The paramter to pass.</param>
        public void Execute(object parameter)
        {
            this.NotifyInstanceHandlers(parameter);
        }

        /// <summary>
        /// Deteremine if the function can be executed.
        /// </summary>
        /// <param name="parameter">The parameter to be passed to the can execute function.</param>
        /// <returns>If the execute function can be executed.</returns>
        public bool CanExecute(object parameter)
        {
            return true;
        }

        #endregion

        #region IDisposable

        /// <summary>
        /// Override in derived classes to clean up specific resources in the concrete class.
        /// </summary>
        protected virtual void OnDispose()
        {
            RemoveHandlers();
        }

        #endregion
    }
}